package com.ethink.mdm.api.component;


import com.ethink.mdm.api.*;
import com.ethink.mdm.api.info.*;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MdmComponent {

    @Autowired
    private MateFacade mateFacade;

    @Autowired
    private BrandFacade brandFacade;

    @Autowired
    private SupplierFacade supplierFacade;

    @Autowired
    private DictionaryFacade dictionaryFacade;

    @Autowired
    private CustomerFacade customerFacade;

    public Map<String, MateSkuInfo> queryMateSkuBySkuCodeList(List<String> mateSkuCodeList) {
        Map<String, MateSkuInfo> map = new HashMap<>();
        if (CollectionUtils.isEmpty(mateSkuCodeList)) {
            return map;
        }
        List<MateSkuInfo> mateSkuInfoList = mateFacade.queryMateSkuBySkuCodeList(mateSkuCodeList);
        if (CollectionUtils.isNotEmpty(mateSkuInfoList)) {
            for (MateSkuInfo mateSkuInfo : mateSkuInfoList) {
                map.put(mateSkuInfo.getMateSku(), mateSkuInfo);
            }
        }
        return map;
    }

    public Map<String, String> queryBrand(Integer mateGroup) {
        Map<String, String> map = new HashMap<>();
        List<BrandInfo> list = brandFacade.listBrandByMateGroup(mateGroup);
        if (CollectionUtils.isNotEmpty(list)) {
            for (BrandInfo info : list) {
                map.put(info.getBrandCode(), info.getBrandName());
            }
        }
        return map;
    }

    public Map<String, String> queryBrand(Integer mateGroup, List<String> codeList) {
        Map<String, String> map = new HashMap<>();
        if (CollectionUtils.isEmpty(codeList)) {
            return map;
        }
        List<BrandInfo> list = brandFacade.listBrandByCodeList(codeList);
        if (CollectionUtils.isNotEmpty(list)) {
            for (BrandInfo info : list) {
                map.put(info.getBrandCode(), info.getBrandName());
            }
        }
        return map;
    }

    public BrandInfo getBrandInfo(String code) {
        List<BrandInfo> list = brandFacade.listBrandByCodeList(Lists.newArrayList(code));
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    public String getBrandName(String code) {
        BrandInfo info = getBrandInfo(code);
        return info == null ? null : info.getBrandName();
    }


    public Map<String, String> querySupplier() {
        Map<String, String> map = new HashMap<>();
        List<SupplierInfo> list = supplierFacade.listSupplier();
        if (CollectionUtils.isNotEmpty(list)) {
            for (SupplierInfo info : list) {
                map.put(info.getSupplierCode(), info.getSupplierName());
            }
        }
        return map;
    }

    public Map<String, String> querySupplier(List<String> codeList) {
        Map<String, String> map = new HashMap<>();
        if (CollectionUtils.isEmpty(codeList)) {
            return map;
        }
        List<SupplierInfo> list = supplierFacade.listSupplierByCodeList(codeList);
        if (CollectionUtils.isNotEmpty(list)) {
            for (SupplierInfo info : list) {
                map.put(info.getSupplierCode(), info.getSupplierName());
            }
        }
        return map;
    }

    public SupplierInfo getSupplier(String code) {
        List<SupplierInfo> list = supplierFacade.listSupplierByCodeList(Lists.newArrayList(code));
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    public String getSupplierName(String code) {
        SupplierInfo info = getSupplier(code);
        return info == null ? null : info.getSupplierName();
    }

    public Map<String, String> queryDictionary(String group) {
        Map<String, String> map = new HashMap<>();
        List<DictionaryInfo> list = dictionaryFacade.listDictionaryByGroup(group);
        if (CollectionUtils.isNotEmpty(list)) {
            for (DictionaryInfo info : list) {
                map.put(info.getCode(), info.getName());
            }
        }
        return map;
    }

    public String getDictionaryValue(String group, String code) {
        return queryDictionary(group).get(code);
    }

    public Map<String, CurrencyInfo> queryCurrency() {
        Map<String, CurrencyInfo> map = new HashMap<>();
        List<CurrencyInfo> list = dictionaryFacade.listAllCurrency();
        if (CollectionUtils.isNotEmpty(list)) {
            for (CurrencyInfo info : list) {
                map.put(info.getCode(), info);
            }
        }
        return map;
    }

    public CurrencyInfo getCurrency(String code) {
        return queryCurrency().get(code);
    }

    public Map<String, String> queryCustomer() {
        Map<String, String> map = new HashMap<>();
        List<CustomerInfo> list = customerFacade.listCustomer();
        if (CollectionUtils.isNotEmpty(list)) {
            for (CustomerInfo info : list) {
                map.put(info.getCustomerCode(), info.getCustomerName());
            }
        }
        return map;
    }

    public Map<String, String> queryCustomer(List<String> codeList) {
        Map<String, String> map = new HashMap<>();
        if (CollectionUtils.isEmpty(codeList)) {
            return map;
        }
        List<CustomerInfo> list = customerFacade.listCustomerByCodeList(codeList);
        if (CollectionUtils.isNotEmpty(list)) {
            for (CustomerInfo info : list) {
                map.put(info.getCustomerCode(), info.getCustomerName());
            }
        }
        return map;
    }

    public CustomerInfo getCustomer(String code) {
        List<CustomerInfo> list = customerFacade.listCustomerByCodeList(Lists.newArrayList(code));
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    public String getCustomerName(String code) {
        CustomerInfo info = getCustomer(code);
        return info == null ? null : info.getCustomerName();
    }
}
